package com.offer;

import com.bean.ListNode;
import com.utils.DataUtils;

public class Test22 {

    public ListNode getKthFromEnd(ListNode head, int k) {
        ListNode dummyNode = head;
        while (k > 0) {
            k--;
            dummyNode = dummyNode.next;
        }

        while (head != null && dummyNode != null) {
            head = head.next;
            dummyNode = dummyNode.next;
        }

        return head;
    }

    public static void main(String[] args) {
        Test22 test22 = new Test22();

        ListNode listNode = DataUtils.getOneToNumberListNode(20);
        DataUtils.printListNode(listNode);
        ListNode result = test22.getKthFromEnd(listNode, 3);
        DataUtils.printListNode(result);
    }

}
